﻿using Infrastructure;
using Lan.Infrastructure.CameraOnvif;
using Lan.Repository;
using Lan.ServiceCore.IService;
using Lan.ServiceCore.Onvif;
using Model;
using System.Runtime.InteropServices;

namespace Lan.ServiceCore.Services
{
    [AppService(ServiceType = typeof(ICalibrationService), ServiceLifetime = LifeTime.Transient)]
    public class CalibrationService : Repository<Calibration>, ICalibrationService
    {
        ONVIF_COMMON_INFO common = new ONVIF_COMMON_INFO();
        List<OnvifManage_COMMON_INFO> list_common = new List<OnvifManage_COMMON_INFO>();


        public CalibrationService()
        {
            list_common = OnvifManage._common;
        }



        public int AddCalibration(Calibration model)
        {
            Queryable().Where(f => f.CameraIp == model.CameraIp && f.DefenceareaId == model.DefenceareaId).ToList().ForEach(item => { Delete(item.Id); });
            list_common.ForEach(item => { if (item.ip == model.CameraIp) { common = item.COMMON_INFO; } });


            ONVIF_PTZ_STATUS _ONVIF_PTZ_STATUS = new ONVIF_PTZ_STATUS();
            int nSizeOfPerson1 = Marshal.SizeOf(_ONVIF_PTZ_STATUS);
            IntPtr intPtr = Marshal.AllocHGlobal(nSizeOfPerson1);
            Marshal.StructureToPtr(_ONVIF_PTZ_STATUS, intPtr, true);
            int ret = onvifsdk.ONVIF_PTZ_GetStatus(2, ref common, intPtr);
            Marshal.FreeHGlobal(intPtr);
            ONVIF_PTZ_STATUS panPost = (ONVIF_PTZ_STATUS)Marshal.PtrToStructure(intPtr, typeof(ONVIF_PTZ_STATUS));
            model.CamerarPointAngle = panPost.panPosition;
            Insertable(model).ExecuteReturnEntity();
            return 0;
        }

        public void cameraPTZ(string _CameraPTZ, string ip)
        {

            ONVIF_PTZ_CONTINUSMOVE ONVIF_PTZ_CONTINUSMOVE = new ONVIF_PTZ_CONTINUSMOVE();
            list_common.ForEach(item => { if (item.ip == ip) { common = item.COMMON_INFO; } });
            float onvif_speed = 1.0f;

            switch (_CameraPTZ)
            {
                case "top":
                    ONVIF_PTZ_CONTINUSMOVE.panSpeed = 0;
                    ONVIF_PTZ_CONTINUSMOVE.tiltSpeed = onvif_speed;// 0.5f;
                    int ret = onvifsdk.ONVIF_PTZ_ContinusMove(2, ref common, ONVIF_PTZ_CONTINUSMOVE);
                    break;
                case "topright":
                    ONVIF_PTZ_CONTINUSMOVE.panSpeed = onvif_speed;//0.5f;
                    ONVIF_PTZ_CONTINUSMOVE.tiltSpeed = onvif_speed;//0.5f;
                    ret = onvifsdk.ONVIF_PTZ_ContinusMove(2, ref common, ONVIF_PTZ_CONTINUSMOVE);
                    break;
                case "right":
                    ONVIF_PTZ_CONTINUSMOVE.panSpeed = onvif_speed;//0.5f;
                    ONVIF_PTZ_CONTINUSMOVE.tiltSpeed = 0;
                    ret = onvifsdk.ONVIF_PTZ_ContinusMove(2, ref common, ONVIF_PTZ_CONTINUSMOVE);
                    break;
                case "bottomright":
                    ONVIF_PTZ_CONTINUSMOVE.panSpeed = onvif_speed;//0.5f;
                    ONVIF_PTZ_CONTINUSMOVE.tiltSpeed = -onvif_speed;// -0.5f;
                    ret = onvifsdk.ONVIF_PTZ_ContinusMove(2, ref common, ONVIF_PTZ_CONTINUSMOVE);
                    break;
                case "bottom":
                    ONVIF_PTZ_CONTINUSMOVE.panSpeed = 0;
                    ONVIF_PTZ_CONTINUSMOVE.tiltSpeed = -onvif_speed;//-0.5f;
                    ret = onvifsdk.ONVIF_PTZ_ContinusMove(2, ref common, ONVIF_PTZ_CONTINUSMOVE);
                    break;
                case "bottomleft":
                    ONVIF_PTZ_CONTINUSMOVE.panSpeed = -onvif_speed;//-0.5f;
                    ONVIF_PTZ_CONTINUSMOVE.tiltSpeed = -onvif_speed;//-0.5f;
                    ret = onvifsdk.ONVIF_PTZ_ContinusMove(2, ref common, ONVIF_PTZ_CONTINUSMOVE);
                    break;
                case "back":
                    ONVIF_PTZ_CONTINUSMOVE.panSpeed = -onvif_speed;//-0.5f;
                    ONVIF_PTZ_CONTINUSMOVE.tiltSpeed = 0;
                    ret = onvifsdk.ONVIF_PTZ_ContinusMove(2, ref common, ONVIF_PTZ_CONTINUSMOVE);
                    break;
                case "topleft":
                    ONVIF_PTZ_CONTINUSMOVE.panSpeed = -onvif_speed;//-0.5f;
                    ONVIF_PTZ_CONTINUSMOVE.tiltSpeed = onvif_speed;//0.5f;
                    ret = onvifsdk.ONVIF_PTZ_ContinusMove(2, ref common, ONVIF_PTZ_CONTINUSMOVE);
                    break;
                case "outin":
                    ONVIF_PTZ_CONTINUSMOVE.panSpeed = 0;
                    ONVIF_PTZ_CONTINUSMOVE.tiltSpeed = 0;
                    ONVIF_PTZ_CONTINUSMOVE.zoomSpeed = -1;
                    ret = onvifsdk.ONVIF_PTZ_ContinusMove(2, ref common, ONVIF_PTZ_CONTINUSMOVE);
                    break;
                case "in":
                    ONVIF_PTZ_CONTINUSMOVE.panSpeed = 0;
                    ONVIF_PTZ_CONTINUSMOVE.tiltSpeed = 0;
                    ONVIF_PTZ_CONTINUSMOVE.zoomSpeed = 1;
                    ret = onvifsdk.ONVIF_PTZ_ContinusMove(2, ref common, ONVIF_PTZ_CONTINUSMOVE);
                    break;

            }
            onvifsdk.ONVIF_PTZ_ContinusStop(2, ref common);
        }

        public void PtzStop()
        {
            onvifsdk.ONVIF_PTZ_ContinusStop(2, ref common);
        }


        public Calibration GetInfo(string CameraIp, int DefenceareaId)
        {
            var response = Queryable()
                .Where(x => x.CameraIp == CameraIp && x.DefenceareaId == DefenceareaId)
                .First();
            return response;
        }


        public void SetTrack(bool Status)
        {
            //if (Status)
            //    TargetCommon.TrackStatus = false;
            //else
            //    TargetCommon.TrackStatus = true;
        }
    }
}
